package com.school.graduation.mine.view;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.school.graduation.R;
import com.school.graduation.common.base.BaseActivity;
import com.school.graduation.mine.presenter.DetailsAdapter;
import com.school.graduation.model.Goods;
import com.school.graduation.model.Student;
import com.school.graduation.util.ToastUtil;

import java.util.List;

import butterknife.Bind;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;

public class MineDetailsActivity extends BaseActivity{

    public static final int RELEASE = 1;
    public static final int SELLOUT = 2;
    public static final int RECEIVE = 3;
    @Bind(R.id.toolBar_modify)
    Toolbar toolBarModify;
    @Bind(R.id.recycler_mine_detail)
    RecyclerView recyclerMineDetail;
    @Bind(R.id.empty_view)
    TextView emptyView;

    private DetailsAdapter adapter;
    int flag;

    public static void jumpToMineOrderDetails(Context context,Bundle bundle){
        Intent intent = new Intent(context,MineDetailsActivity.class);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    public int getContentView() {
        return R.layout.activity_mine_details;
    }

    @Override
    public void initData() {
        initToolbar();
        initRecycler();
        flag = getIntent().getIntExtra("mine",-1);
        initRecyclerData(flag);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initRecyclerData(flag);
    }

    private void initRecyclerData(int flag) {
        if (flag==-1) return;
        switch (flag){
            case RELEASE:
                getReleaseGoods();
                break;
        }
    }

    //我发布的
    private void getReleaseGoods() {
        BmobQuery<Goods> queryGoods = new BmobQuery<>();
        queryGoods.addWhereEqualTo("studentIssue", BmobUser.getCurrentUser(Student.class));
        queryGoods.include("studentIssue");
        queryGoods.setLimit(1000);
        queryGoods.order("-createdAt");
        queryGoods.findObjects(new FindListener<Goods>() {
            @Override
            public void done(List<Goods> list, BmobException e) {
                if(e==null){
                    setAdapter(list);
                }else ToastUtil.makeShortToast(context,"获取数据出错");
            }
        });
    }

    private void setAdapter(List<Goods> list) {
        if (list.size()==0) emptyView.setVisibility(View.VISIBLE);
        recyclerMineDetail.setAdapter(adapter);
    }

    private void initToolbar() {
        toolBarModify.setTitle("详情");
        toolBarModify.setTitleTextColor(getResources().getColor(R.color.md_white_1000));
    }

    private void initRecycler() {
        LinearLayoutManager manager = new LinearLayoutManager(context);
        recyclerMineDetail.setLayoutManager(manager);
    }
}
